<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <router-link to="/home">主页</router-link>
        <router-link to="/product/123">产品详情1</router-link>
        <router-link to="/product/456">产品详情2</router-link>
        <router-view></router-view>
    </div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<script src="./node_modules/vue-router/dist/vue-router.js"></script>
<script>
    let routes = [
        {
            path: "/home",
            beforeEnter: (to, from, next) => {
                console.log("/home路由的导航守卫");
                next();
            },
            component: () => Promise.resolve({
                template: `<h1>主页</h1>`,
                beforeRouteEnter(to, from, next) {
                    console.log("进入到主页的路由");
                    console.log(this);
                    next();
                },
                beforeRouteUpdate(to, from, next) {
                    console.log("主页路由更新的时候");
                    console.log(this);
                    next();

                },
                beforeRouteLeave(to, from, next) {
                    console.log("离开主页路由");
                    next();
                }

            })
        },
        {
            path: "/product/:productId",
            beforeEnter: (to, from, next) => {
                console.log("/product路由的导航守卫");
                next();
            },
            component: () => import("./Product.js")
        },
    ]
    let router = new VueRouter({
        routes
    })
    router.beforeEach((to, from, next) => {
        console.log("执行全局的前置的导航守卫");
        next();
    })
    let vm = new Vue({
        router,
        el: "#app"
    })
</script>

</html>